package cn.edu.scau.cmi.wuzhenzhou.service;

import java.util.Scanner;

import org.apache.ibatis.session.SqlSession;

import cn.edu.scau.cmi.wuzhenzhou.dao.StudentDao;
import cn.edu.scau.cmi.wuzhenzhou.dao.TeacherDao;
import cn.edu.scau.cmi.wuzhenzhou.domain.Student;
import cn.edu.scau.cmi.wuzhenzhou.domain.Teacher;
import cn.edu.scau.cmi.wuzhenzhou.util.ApplicationContextUtil;
import cn.edu.scau.cmi.wuzhenzhou.util.SessionFactoryUtil;

public class TeacherService {

	private static SqlSession sqlSession;
	
	private static StudentDao studentDao;
	private static TeacherDao teacherDao;
	
	private static Scanner in;
	
	public TeacherService() throws Exception{
		in = new Scanner(System.in);
		sqlSession = SessionFactoryUtil.getSqlSession();
		studentDao = sqlSession.getMapper(StudentDao.class);
		teacherDao = sqlSession.getMapper(TeacherDao.class);
	}
	
	/**
	 * 添加教师
	 */
	public void addTeacher() {
		Teacher teacher = ApplicationContextUtil.getApplicationContext().getBean("teacher",Teacher.class);
		System.out.println("请输入教师的id：");
		teacher.setId(in.nextLong());
		System.out.println("请输入教师的名字:");
		teacher.setName(in.next());
		teacherDao.addTeacher(teacher);
		sqlSession.commit();
		System.out.println(teacherDao.selectAll());
	}
	
	/**
	 * 删除教师
	 */
	public void deleteTeacher() {
		System.out.println("请输入要删除教师的id:");
		Long id = in.nextLong();
		teacherDao.deleteTeacherById(id);
		sqlSession.commit();
		System.out.println(teacherDao.selectAll());
	}
	
	/**
	 * 修改教师
	 */
	public void updateTeacher() {
		System.out.println("请输入要更新教师的id");
		Long id = in.nextLong();
		Teacher teacher = teacherDao.selectTeacherById(id);
		if(teacher==null) System.out.println("不存在该教师");
		else {
			System.out.println(teacher);
			System.out.println("请输入要更新教师的名字:");
			teacher.setName(in.next());
			teacherDao.updateTeacherById(teacher);
			System.out.println(teacherDao.selectTeacherById(id));
		}
	}
	
	/**
	 * 老师选择被指导的学生
	 */
	public void addStudentOfTeacher() {
		System.out.println("请输入教师的id:");
		Teacher teacher = teacherDao.selectTeacherById(in.nextLong());
		if(teacher==null) System.out.println("该教师不存在");
		else {
			System.out.println(studentDao.selectAll());
			System.out.println("请输入选择学生的id:");
			Long id = in.nextLong();
			Student student = studentDao.selectStudentById(id);
			if(student==null) System.out.println("该学生不存在");
			else {
				student.setId(id);
				student.setTeacher(teacher);
				studentDao.updateTeacherOfStudent(student);
				sqlSession.commit();
				System.out.println(teacherDao.selectStudentByTeacherId(teacher.getId()));
			}
		}
	}
	
	/**
	 * 教师修改被指导的学生
	 */
	public void updateTeacherOfStudent() {
		System.out.println("请输入教师的id:");
		Teacher teacher = teacherDao.selectTeacherById(in.nextLong());
		if(teacher==null) System.out.println("该教师不存在");
		else {
			System.out.println(teacherDao.selectStudentByTeacherId(teacher.getId()));
			System.out.println("请输入要修改的学生id:");
			Student student = ApplicationContextUtil.getApplicationContext().getBean("student",Student.class);
			student.setId(in.nextLong());
			teacherDao.deleteTeacherOfStudent(student);
			sqlSession.commit();
			System.out.println(teacherDao.selectStudentByTeacherId(teacher.getId()));
		}
	}

}

